package com.zyx.concurrentdemo.mytest.c04;


import com.zyx.concurrentdemo.c02basic.util.HeaderPrint;
import lombok.extern.slf4j.Slf4j;
import org.openjdk.jol.info.ClassLayout;

import java.util.Vector;
import java.util.concurrent.locks.LockSupport;

import static com.zyx.concurrentdemo.c02basic.util.Sleeper.sleep;

/**
 * @author Yaxi.Zhang
 * @since 2021/10/14 12:12
 * desc: 测试偏向锁
 * -XX:-UseCompressedOops -XX:-UseCompressedClassPointers -XX:BiasedLockingStartupDelay=0 -XX:+PrintFlagsFinal
 * -XX:-UseBiasedLocking tid=0x000000001f173000  -XX:BiasedLockingStartupDelay=0 -XX:+TraceBiasedLocking
 *      -XX:BiasedLockingStartupDelay=0 默认情况下偏向锁的实际生效时间是有延迟的, 该参数可以将偏向锁生效时间设置为0, 程序开始时即生效
 *      -XX:UseBiasedLocking=false/-XX:-UseBiasedLocking  不使用偏向锁
 *      -XX:+UseHeavyMonitors 使用重量级锁
 */
@Slf4j(topic = "c.BiasedDemo")
public class BiasedDemo {

    private static Thread t1, t2, t3;

    public static void main(String[] args) throws InterruptedException {
        test4();
    }

    /**
     * 测试偏向锁
     */
    private static void test1() {
        Dog d = new Dog();
        log.debug(HeaderPrint.printHeader(d));

        try {
            Thread.sleep(4000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        log.debug(HeaderPrint.printHeader(new Dog()));
    }

    private static void test5() throws InterruptedException {
        log.debug("begin");
        for (int i = 0; i < 6; i++) {
            Dog d = new Dog();
            log.debug(ClassLayout.parseInstance(d).toPrintable());
            Thread.sleep(1000);
        }
    }

    /**
     * 禁用偏向锁:-XX:UseBiasedLocking=false/-XX:-UseBiasedLocking
     */
    private static void test2() throws InterruptedException {

        Dog d = new Dog();
        Thread t1 = new Thread(() -> {
            synchronized (d) {
                log.debug(HeaderPrint.printHeader(d));
            }
            synchronized (BiasedDemo.class) {
                BiasedDemo.class.notify();
            }
        }, "t1");
        t1.start();

        Thread t2 = new Thread(() -> {
            synchronized (BiasedDemo.class) {
                try {
                    BiasedDemo.class.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            log.debug(HeaderPrint.printHeader(d));
            synchronized (d) {
                log.debug(HeaderPrint.printHeader(d));
            }
            log.debug(HeaderPrint.printHeader(d));
        }, "t2");
        t2.start();
    }

    /**
     * 批量重偏向, 当撤销超过20次后（超过阈值）, 且偏向另一线程, 则锁会重偏向
     */
    private static void test3() throws InterruptedException {
        Vector<Dog> list = new Vector<>();
        Thread t1 = new Thread(() -> {
            for (int i = 0; i < 30; i++) {
                Dog d = new Dog();
                list.add(d);
                synchronized (d) {
                    log.debug(i + "\t" + HeaderPrint.printHeader(d));
                }
            }
            synchronized (list) {
                list.notify();
            }
        }, "t1");
        t1.start();

        Thread t2 = new Thread(() -> {
            synchronized (list) {
                try {
                    list.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            log.debug("===============> ");
            for (int i = 0; i < 30; i++) {
                Dog d = list.get(i);
                log.debug(i + "\t" + HeaderPrint.printHeader(d));
                synchronized (d) {
                    log.debug(i + "\t" + HeaderPrint.printHeader(d));
                }
                log.debug(i + "\t" + HeaderPrint.printHeader(d));
            }
        }, "t2");
        t2.start();
    }



    private static void test4() throws InterruptedException {
        Vector<Dog> list = new Vector<>();

        int loopNumber = 39;
        t1 = new Thread(() -> {
            for (int i = 0; i < loopNumber; i++) {
                Dog d = new Dog();
                list.add(d);
                synchronized (d) {
                    log.debug(i + "\t" + HeaderPrint.printHeader(d));
                }
            }
            LockSupport.unpark(t2);
        }, "t1");
        t1.start();

        t2 = new Thread(() -> {
           LockSupport.park();
            log.debug("===============> ");
            for (int i = 0; i < loopNumber; i++) {
                Dog d = list.get(i);
                log.debug(i + "\t" + HeaderPrint.printHeader(d));
                synchronized (d) {
                    log.debug(i + "\t" + HeaderPrint.printHeader(d));
                }
                log.debug(i + "\t" + HeaderPrint.printHeader(d));
            }
            LockSupport.unpark(t3);
        }, "t2");
        t2.start();

        t3 = new Thread(() -> {
            LockSupport.park();
            log.debug("===============> ");
            for (int i = 0; i < loopNumber; i++) {
                Dog d = list.get(i);
                log.debug(i + "\t" + HeaderPrint.printHeader(d));
                synchronized (d) {
                    log.debug(i + "\t" + HeaderPrint.printHeader(d));
                }
                log.debug(i + "\t" + HeaderPrint.printHeader(d));
            }
        }, "t3");
        t3.start();
        t3.join();
        log.debug(HeaderPrint.printHeader(new Dog()));
        log.debug(HeaderPrint.printHeader(new Dog()));
    }

    private static void test6() throws InterruptedException {
        Dog d = new Dog();
        Object obj = new Object();

        int loopNumber = 50;

        t1 = new Thread(() -> {
            synchronized (obj) {
                for (int i = 0; i < loopNumber; i++) {
                    sleep(1);
                    log.debug(i + "\t" + HeaderPrint.printHeader(d));
                    obj.notifyAll();
                    synchronized (d) {
                        log.debug(i + "\t" + HeaderPrint.printHeader(d));
                    }
                    log.debug(i + "\t" + HeaderPrint.printHeader(d));
                    try {
                        obj.wait(3000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }

        }, "t1");
        t1.start();

        t2 = new Thread(() -> {
            log.debug("===============> ");
            synchronized (obj) {
                for (int i = 0; i < loopNumber; i++) {
                    sleep(0.01);
                    log.debug(i + "\t" + HeaderPrint.printHeader(d));
                    obj.notifyAll();
                    synchronized (d) {
                        log.debug(i + "\t" + HeaderPrint.printHeader(d));
                    }
                    log.debug(i + "\t" + HeaderPrint.printHeader(d));
                    try {
                        obj.wait(3000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }, "t2");
        t2.start();

        t3 = new Thread(() -> {
            log.debug("===============> ");
            synchronized (obj) {
                for (int i = 0; i < loopNumber; i++) {
                    sleep(0.01);
                    log.debug(i + "\t" + HeaderPrint.printHeader(d));
                    obj.notifyAll();
                    synchronized (d) {
                        log.debug(i + "\t" + HeaderPrint.printHeader(d));
                    }
                    log.debug(i + "\t" + HeaderPrint.printHeader(d));
                    try {
                        obj.wait(3000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }, "t2");
        t3.start();

        t1.join();
        t2.join();
        t3.join();

        log.debug(HeaderPrint.printHeader(new Dog()));
        log.debug(HeaderPrint.printHeader(new Dog()));

    }

}

class Dog {

}


